package com.darkblue.model;

import java.util.Map;
import java.util.Set;





public class EntityModel {

	public static final String mapperModel= ""
			+ "package {Z}"
			+ "@MapperScannerAnnotation "
			+ "public interface {X}Mapper {"
				+ "public void add{X}({X}Entity {Y}Entity);"
				+ "public void del{X}(String id);"
				+ "public {X}Vo findAll{X}();"
				+ "public int update{X}({X}Entity {Y}Entity);"
			+ "}";
	
	
	public static final String serviceModel = ""
			+ "package {Z}"
			+ "@Service "
			+ "public class {X}Service {"
				+ "@Autowired "
				+ "private {X}Mapper {Y}Mapper;"
				+ "public void add{X}({X}Entity {Y}Entity){"
					+ "this.{Y}Mapper.add{X}({Y}Entity);"
				+ "}"
				+ "public void del{X}(String id){"
					+ "this.{Y}Mapper.del{X}(id);"
				+ "}"
				+ "public {X}Vo findAll{X}(){"
					+ "return this.{Y}Mapper.findAll{X}();"
				+ "}"
				+ "public int update{X}({X}Entity {Y}Entity){"
					+ "return this.{Y}Mapper.update{X}({Y}Entity);"
				+ "}"
			+ "}";
	
	public static final String controllerModel = ""
			+ "package {Z}"
			+ "@Controller "
			+ "@RequestMapping(\"/{Y}\")"
			+ "public class {X}Controller {"
				+ "@Autowired "
				+ "private {X}Service {Y}Service;"
				+ "@ResponseBody "
				+ "public void add{X}({X}Entity {Y}Entity){"
					+ "this.{Y}Service.add{X}({Y}Entity);"
				+ "}"
			+ "}";
	
	public static final String entityModel = ""
			+ "package {Z}"
			+ "public class {X}Entity {"
				+ "{Y}"
			+ "}";
	
	public static final String voModel = ""
			+ "package {Z}"
			+ "public class {X}Vo {"
				+ "{Y}"
			+ "}";
	
	public static String getMapperModel(String entityName,String pagePath){
		String[] packs = pagePath.split("/");
		StringBuffer sb = new StringBuffer();
		for (String pack : packs) {
			sb.append(pack).append(".");
		}
		char[] cs=entityName.toCharArray();
		cs[0]+=32;
		return mapperModel.replace("{X}", entityName).replace("{Y}", String.valueOf(cs)).replace("{Z}", sb.append("mapper;").toString());
	}
	
	
	public static String getServiceModel(String entityName,String pagePath){
		String[] packs = pagePath.split("/");
		StringBuffer sb = new StringBuffer();
		for (String pack : packs) {
			sb.append(pack).append(".");
		}
		char[] cs=entityName.toCharArray();
		cs[0]+=32;
		return serviceModel.replace("{X}", entityName).replace("{Y}", String.valueOf(cs)).replace("{Z}", sb.append("service;").toString());
	}
	
	public static String getControllerModel(String entityName,String pagePath){
		String[] packs = pagePath.split("/");
		StringBuffer sb = new StringBuffer();
		for (String pack : packs) {
			sb.append(pack).append(".");
		}
		char[] cs=entityName.toCharArray();
		cs[0]+=32;
		return controllerModel.replace("{X}", entityName).replace("{Y}", String.valueOf(cs)).replace("{Z}", sb.append("controller;").toString());
	}
	
	public static String getEntityModel(String entityName,Map<String, String> columnNames,String pagePath){
		String[] packs = pagePath.split("/");
		StringBuffer sb1 = new StringBuffer();
		for (String pack : packs) {
			sb1.append(pack).append(".");
		}
		StringBuffer sb = new StringBuffer();
		Set<String> colSet = columnNames.keySet();
		for (String name : colSet) {
			sb.append("private ")
			  .append(columnNames.get(name))
			  .append(" ")
			  .append(name)
			  .append(";");
		}
		return entityModel.replace("{X}", entityName).replace("{Y}", String.valueOf(sb)).replace("{Z}", sb1.append("entity;").toString());
	}
	
	public static String getVoModel(String entityName,Map<String, String> columnNames,String pagePath){
		String[] packs = pagePath.split("/");
		StringBuffer sb1 = new StringBuffer();
		for (String pack : packs) {
			sb1.append(pack).append(".");
		}
		StringBuffer sb = new StringBuffer();
		Set<String> colSet = columnNames.keySet();
		for (String name : colSet) {
			sb.append("private ")
			  .append(columnNames.get(name))
			  .append(" ")
			  .append(name)
			  .append(";");
		}
		return voModel.replace("{X}", entityName).replace("{Y}", String.valueOf(sb)).replace("{Z}", sb1.append("vo;").toString());
	}
}
